//获取url中传递的参数
function GetQueryString(name)
{
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)
        return  unescape(r[2]);
    return null;
}


//对用户隐藏接受委托按钮

if (user.user_type == 2) {
    console.log("---------")
    $('#jieshouBTN').css("display", "block");

}


//从url中拿到委托编号并设置页面上该字段的值
document.getElementById('weituobianhao').innerHTML =GetQueryString("attr5")
//从数据库取数据
$.ajax({
    url: '/checkDepute',
    type: 'post',
    data: {'requestType': 'initBaseData', 'task_id': GetQueryString("attr5")},
    success: function (data, status) {
        if (status == 'success') {
            console.log('收到的数据为：' + data)
            document.getElementById('taskPaper_type').innerHTML = data.taskPaper_type   //委托单类型
            document.getElementById('weituodanwei').innerHTML = data.task_company   //委托单位
            document.getElementById('xiangmumingcheng').innerHTML = data.task_pname         //项目名称
            document.getElementById('chanpingmingcheng').innerHTML = data.task_product      //产品名称
            document.getElementById('jianceshiji').innerHTML = data.task_state         //检测时机
            document.getElementById('jiancedidian').innerHTML = data.task_addr         //检测地点
            document.getElementById('jianceshuliang').innerHTML = data.task_workNum         //检测数量
            document.getElementById('jiancebiaozhun').innerHTML = data.task_standard         //检测标准
            document.getElementById('changpinbianhao').innerHTML = data.task_position         //坡口形式
            document.getElementById('weituoren').innerHTML = data.task_client         //委托人
            document.getElementById('lianxidianhua').innerHTML = data.task_phone         //联系电话
            document.getElementById('weituoshuoming').innerHTML = data.task_description   //联系电话
        }
    },
    error: function (data, status) {
        if (status == 'error') {
        }
    }
});

//具体每个工件的信息：[w_pipeNum, w_weldNum, w_pipeName, w_scale,  w_spec, w_texture, w_weldM, w_test, w_qua]
var workpieceInfos = []
var workpieceNum
//post请求后台查询该委托单下的工件信息
$.ajax({
    url: '/checkDepute',
    type: 'post',
    data: {'requestType': 'initWorkPieceData', 'w_tid': GetQueryString("attr5")},
    async: false,
    success: function (data, status) {
        if (status == 'success') {
            console.log('共取到 ' + data.length + ' 条工件数据')
            workpieceNum = data.length
            //将取到的所有数据循环添加到workpieceInfos中
            for (var i = 0; i < data.length; i++){
                workpieceInfos[i] = [data[i].w_pipeNum, data[i].w_weldNum, data[i].w_pipeName, data[i].w_scale,
                    data[i].w_spec, data[i].w_texture, data[i].w_weldM, data[i].w_test, data[i].w_qua]
            }
        }
    },
    error: function (data, status) {
        if (status == 'error') {
            // alert('请求错误，刷新后再试')
        }
    }
});

//列数 = 8
var methodCount = 8;
var table = document.getElementById("workpieceInfos");
var tbody = table.children[1];
var inner = "<td>&nbsp;</td>";
for (var j = 0; j < methodCount; j++) {
    inner += "<td />";
}
for (var j = 0; j < workpieceNum; j++) {
    var tr = document.createElement("tr");
    tr.innerHTML = inner;
    tbody.appendChild(tr);
}
//显示数据
for (var i = 0; i < workpieceNum; i++) {
    if (workpieceInfos[i] !== undefined) {
        for (var j = 0; j < methodCount+1; j++) {
            tbody.children[i].children[j].innerHTML = workpieceInfos[i][j];
        }
        var attr = "";
        for (j = 0; j < methodCount+1; j++) {
            attr += j === 0 ? "?" : "&";
            attr += "attr" + j + "=" + workpieceInfos[i][j];
        }
    } else {
        tbody.children[i].children[0].innerHTML = "&nbsp;";
        for (j = 1; j < methodCount+ 1; j++) {
            tbody.children[i].children[j].innerHTML = "";
        }
    }
}

$('#fenpeibumen').click(function () {
    var temp = document.getElementById('jiancebumen')
    var index = temp.selectedIndex
    var value = temp.options[index].value
    console.log('选择分配的部门为：' + value)
    $.ajax({
        url: '/checkDepute',
        type: 'post',
        data: {'requestType': 'updateTask', 'task_department': value, 'task_id': 21},
        success: function (data, status) {
            if (status == 'success') {
               alert('分配成功！')
            }
        },
        error: function (data, status) {
            if (status == 'error') {
                alert('分配失败！')
            }
        }
    });
})







